import Base from "./base";
import fs from "fs";
const path = require("path");
import SocketsXinghuo from "../../utils/chatxinghuo";
import SocketsXfVoice from "../../utils/xfVoice";
import { gptChat } from "../../utils/gpt";
export default class extends Base {
  seffndAction() {}
  async onGptChat(res: any, msg: string) {
    return new Promise(async (resolve, reject) => {
      await gptChat(msg, (datas: any) => {
        const { data, status } = datas;
        let i = 0;

        if (data) {
          res.write(data);
        }
      })
        .then((res) => {
          console.log(res);

          resolve({});
        })
        .catch(() => {
          console.log("到消息页面失败");

          reject();
        });
    });
  }
  async xinghuoAction() {
    const { res, req } = this.ctx;
    this.setSSE();
    const msg = this.post("msg");

    res.end();
  }
  moni(res) {
    let i = 0;
    return new Promise((resolve) => {
      const timer = setInterval(() => {
        i++;
        res.write("aaaa");
        console.log(i);

        if (i > 10) {
          clearInterval(timer);
          res.end("消息获取完成");
          resolve({});
        }
      }, 500);
    });
  }
  async getFileText(): Promise<string> {
    return new Promise((resolve, reject) => {
      const paths = path.join(think.ROOT_PATH, "runtime/upload/16k_10.pcm");
      var readerStream = fs.createReadStream(paths, {
        highWaterMark: 1028,
      });
      let arr: Buffer[] = [];
      readerStream.on("data", function (chunk) {
        arr.push(chunk);
      });
      const xinghuo = new SocketsXfVoice();
      // 最终帧发送结束
      readerStream.on("end", async function () {
        const xhMsgs = await xinghuo
          .send(Buffer.concat([...arr]))
          .then((str) => {
            resolve(str);
          })
          .catch((err) => {});
      });
    });
  }
  async getVoiceText(msg: string): Promise<string> {
    return new Promise((resolve, reject) => {
      const decodedData = Buffer.from(msg, "base64");
      const xinghuo = new SocketsXfVoice();
      xinghuo
        .send(decodedData)
        .then((str) => {
          resolve(str);
        })
        .catch((err) => {
          reject(err);
        });
    });
  }

  async voiceAction() {
    const { req, res } = this.ctx;
    this.setSSE();
    const msg = this.post("msg");
    const str = await this.getVoiceText(msg);
    if (!str) return res.end("识别错误");
    res.write('您的问题:'+str+':\r\n\r\n');
    const wz = await this.getAi(str, res);
    res.end("");
  }
  async getAi(msg: string, res?: any) {
    return new Promise(async (resolve, reject) => {
      const msgS = await this.onGptChat(res, msg).catch(() => {});

      if (!msgS) {
        // console.log("失败后开始返回");
        res.write("gpt消息获取失败,接下来由星火为您解答：\r\n");
        const xinghuo = new SocketsXinghuo({
          callback: (e: string) => {
            res && res.write(e);
          },
        });
        const xhMsgs = await xinghuo.send(msg).catch(() => {});
        if (!xhMsgs) {
          return res.end("消息获取失败");
        }
      } else {
        resolve(msgS);
      }
    });
  }
  async sendAction() {
    const { req, res } = this.ctx;
    const msg = this.post("msg");
    console.log(msg);
    this.setSSE();
    const data = await this.getAi(msg, res);
  }
}
